package com.engine.jucailinkq.attendance.component.persongroup.job.scheduling.tactics;

import com.engine.jucailinkq.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.jucailinkq.attendance.enums.CheckBoxEnum;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 轮转周期为年
 */
@Slf4j
public class YearRegularScheduling implements RegularScheduling{
    @Override
    public boolean support(int personClassfiy) {
        if (personClassfiy == 3){
            return true;
        }
        return false;
    }

    @Override
    public void compute(Map<String, Object> data) {
        log.debug("import YearRegularScheduling :[{}]",data);
        //主表数据id
        String id = Util.null2String(data.get("id"));
        //规律排班模块Id
        String modeId = Util.null2String(data.get("modeId"));
        //排班结果模块Id
        String formmodeid = Util.null2String(data.get("formmodeid"));
        //生成天数
        int day = Integer.valueOf(Util.null2String(data.get("day")));
        //企业日历
        String qyrl = Util.null2String(data.get("qyrl"));
        //节假日处理 0:改班，1：跳过，2：不处理
        String jjrcl = Util.null2String(data.get("jjrcl"));
        //公休日处理 0：改班，1：跳过，2：不处理
        String gxrcl = Util.null2String(data.get("gxrcl"));
        //生效日期
        String bdate = Util.null2String(data.get("bdate"));
        //当前日期
        String nowDate = DateUtil.getCurrentDate();
        //排班方式
        String pbfs = Util.null2String(data.get("pbfs"));
        //改班班次
        String gbbc = Util.null2String(data.get("gbbc"));
        //改班班次是否休息
        String gbbcIfRest = CommonUtil.ifrestByClass(gbbc);
        //失效日期
        String edate = Util.null2String(data.get("edate"));
        //执行日期
        String executeDate = Util.null2String(data.get("executeDate"));
        //改班候选班组
        String gbhxbz = Util.null2String(data.get("gbhxbz"));


        //规律排班明细表数据
        String sql = "select a.ksrq,a.jsrq,a.hxbz,a.bcmc,b.sfxx,b.bcsdxx from uf_jcl_kq_glpb_dt1 a left join uf_jcl_kq_bcxx b on a.bcmc=b.id where a.mainid=?";
        List<Map<String,Object>> detailDataList = DbTools.getSqlToList(sql,id);
        if (detailDataList.size() == 0){
            return;
        }
        //规律排班适用组织数据
        sql = "select dx,dxlx,aqjb from uf_jcl_syzz where modeid=? and dataid=?";
        List<Map<String,Object>> organizationList = DbTools.getSqlToList(sql,modeId,id);
        //规律排班所用企业日历数据
        sql = "select rlmc,nd,rq,rqlx from uf_jcl_kq_rlxx where rlmc=?";
        List<Map<String,Object>> calendarList = DbTools.getSqlToList(sql,qyrl);
//        Map<String,String> calendarMap = calendarList.stream().collect(Collectors.toMap(e->Util.null2String(e.get("nd")),e->Util.null2String(e.get("id"))));
//        log.debug("calendarMap : {}",calendarMap);
        Map<String, List<Map<String,Object>>> detailDataGroupingByYear = calendarList.stream().collect(Collectors.groupingBy(e ->e.get("nd").toString()));
        // int betweenDays = CommonUtil.getBetweenDays(data);
        //星期
        // int circulateDays = Integer.valueOf(detailDataList.get(detailDataList.size()-1).get("jsxq").toString()) +1;

        LocalDateTime  leaveDay = null;;
        int count = 0;
        List<Map<String,Object>> insertDataList = Lists.newArrayList();
        while (insertDataList.size() <=day){
            String bcrq = DateUtil.AfterDay(executeDate,count);
            if (DateUtil.getBetWeenDays(bcrq,edate) <=0){
                //超过规律有效期
                break;
            }
            leaveDay = DateUtil.getTime(bcrq);
            for (Map<String,Object> map : detailDataList){
                String ksrq = Util.null2String(map.get("ksrq"));
                String jsrq = Util.null2String(map.get("jsrq"));
                Map<String,Object> dataMap = Maps.newHashMap();
                dataMap.put("pbtj","0");
                dataMap.put("pbgl",id);
                dataMap.put("bcrq",bcrq);
                LocalDateTime beginDate = DateUtil.getTime(leaveDay.getYear()+"-"+ksrq);
                LocalDateTime endDate = DateUtil.getTime(leaveDay.getYear()+"-"+jsrq);
                if (DateUtil.getBetWeenDays(leaveDay.getYear()+"-"+ksrq,leaveDay.getYear()+"-"+jsrq) <0){
                    beginDate = DateUtil.getTime(leaveDay.getYear()+"-"+ksrq);
                    endDate = DateUtil.getTime((leaveDay.getYear()+1)+"-"+jsrq);
                }
                if (endDate.compareTo(leaveDay) >=0 && beginDate.compareTo(leaveDay) <=0){
                    String dateTime =bcrq;
                    log.debug("dateTime :{},count: {},map : {}",dateTime,count,map);
                    List<Map<String,Object>> detailList = detailDataGroupingByYear.get(dateTime.split("-")[0]);
                    if (detailList == null){
                        continue;
                    }
                    List<Map<String,Object>> nowDateList = detailList.stream().filter(e->dateTime.equals(Util.null2String(e.get("rq")))).collect(Collectors.toList());
                    if (nowDateList.size() ==0){
                        continue;
                    }
                    Map<String,Object> nowDateMap = nowDateList.get(0);
                    //日期类型
                    String rqlx = Util.null2String(nowDateMap.get("rqlx"));
                    dataMap.put("rqlx",rqlx);
                    String sfxx = Util.null2String(map.get("sfxx")).equals("")? CheckBoxEnum.UNCHECKED.getKey():Util.null2String(map.get("sfxx"));
                    dataMap.put("sfxx",sfxx);
                    if ("0".equals(pbfs)){
                        //固定班次
                        dataMap.put("sfdkpp","0");
                        dataMap.put("hxbz",null);
                        dataMap.put("bcxx",map.get("bcmc"));
                        dataMap.put("bcbdsj",map.get("bcsdxx"));
                    }else if ("1".equals(pbfs)){
                        //打卡匹配
                        dataMap.put("sfdkpp","1");
                        dataMap.put("hxbz",map.get("hxbz"));
                        dataMap.put("bcxx",null);
                        dataMap.put("bcbdsj",null);
                    }
                    dataMap.put("pbrq",DateUtil.getCurrentDate());
                    dataMap.put("pbsj",DateUtil.getCurrentTime("yyyy-MM-dd HH:mm").split(" ")[1]);
                    if ("1".equals(rqlx)){
                        //当为节假日时
                        if ("0".equals(jjrcl)){
                            //改班
                            if (("".equals(gbbc) && "0".equals(pbfs)) || ("".equals(gbhxbz) && "1".equals(pbfs))){
                                //休息
                                dataMap.put("sfxx","1");
                                dataMap.put("bcxx",null);
                                dataMap.put("hxbz",null);
                            }else {
                                if ("0".equals(pbfs)){
                                    //固定班次
                                    dataMap.put("bcxx",gbbc);
                                    dataMap.put("hxbz",null);
                                    dataMap.put("sfxx",gbbcIfRest);
                                }else {
                                    //打卡匹配
                                    dataMap.put("hxbz",gbhxbz);
                                    dataMap.put("bcxx",null);
                                    dataMap.put("sfxx",gbbcIfRest);
                                }
                            }
                            insertDataList.add(dataMap);
                        }else if ("1".equals(jjrcl)){
                            //跳过
                            dataMap.put("sfxx","1");
                            dataMap.put("bcxx",null);
                            insertDataList.add(dataMap);
                        }else if ("2".equals(jjrcl)){
                            //不处理
                            insertDataList.add(dataMap);
                        }
                    }else if ("2".equals(rqlx) || "3".equals(rqlx)){
                        if ("0".equals(gxrcl)){
                            //改班
                            if (("".equals(gbbc) && "0".equals(pbfs)) || ("".equals(gbhxbz) && "1".equals(pbfs))){
                                //休息
                                dataMap.put("sfxx","1");
                                dataMap.put("bcxx",null);
                                dataMap.put("hxbz",null);
                            }else {
                                if ("0".equals(pbfs)){
                                    //固定班次
                                    dataMap.put("bcxx",gbbc);
                                    dataMap.put("hxbz",null);
                                    dataMap.put("sfxx",gbbcIfRest);
                                }else {
                                    //打卡匹配
                                    dataMap.put("hxbz",gbhxbz);
                                    dataMap.put("bcxx",null);
                                    dataMap.put("sfxx",gbbcIfRest);
                                }
                            }
                            insertDataList.add(dataMap);
                        }else if ("1".equals(gxrcl)){
                            //跳过
                            dataMap.put("sfxx","1");
                            dataMap.put("bcxx",null);
                            insertDataList.add(dataMap);
                        }else if ("2".equals(gxrcl)){
                            //不处理
                            insertDataList.add(dataMap);
                        }
                    } else if ("0".equals(rqlx) || "4".equals(rqlx)){
                        //当为工作日
                        insertDataList.add(dataMap);
                    }

                }
            }
            count++;
        }

        /**插入排班结果表*/
        PersongroupCommonUtil.insertShiftSchedulingResults(organizationList,insertDataList,formmodeid);
        //删除过期数据
        PersongroupCommonUtil.deleteExpireData(id,edate);

    }
}
